package com.zzptc.easyshop.servlet;

import com.zzptc.easyshop.dao.MessageDao;
import com.zzptc.easyshop.vo.Message;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "MessageServlet")
public class MessageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        //获取表
        MessageDao messageDao=new MessageDao();
        List list=null;
        try {
            list=messageDao.getAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //插入

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        Message message=new Message();
        String name=request.getParameter("guestName");
        String title=request.getParameter("guestTitle");
        String content=request.getParameter("guestContent");
        message.setEc_nick_name(name);
        message.setEc_title(title);
        message.setEc_content(content);
        message.setEc_create_time(df.format(new Date()));
        boolean i=false;
        try {
            i=messageDao.insert(message);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (i){
            System.out.println("提交成功");
        }else {
            System.out.println("提交失败");
        }

        request.setAttribute("message",list);
        request.getRequestDispatcher("guestbook.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
